﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BestBuy.DAL;

namespace BestBuy.UserControl
{
    public partial class ProductDetail : System.Web.UI.UserControl
    {
        cBangTinMua cBangTinMua = new cBangTinMua();
        cDonVi cDonVi = new cDonVi();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadComboboxDonVi();
            }
        }

        private void LoadComboboxDonVi()
        {
            cbAnnualPurchaseVolume.DataSource = cDonVi.GetAll();
            cbAnnualPurchaseVolume.DataTextField = "TenDonVi";
            cbAnnualPurchaseVolume.DataValueField = "IDDonVi";
            cbAnnualPurchaseVolume.DataBind();
            cbAnnualPurchaseVolume.SelectedIndex = 0;
            cbQuantityRequired.DataSource = cDonVi.GetAll();
            cbQuantityRequired.DataTextField = "TenDonVi";
            cbQuantityRequired.DataValueField = "IDDonVi";
            cbQuantityRequired.DataBind();
            cbQuantityRequired.SelectedIndex = 0;
        }

        public void LoadCategory(int ID)
        {
            lbCategory.Text = cBangTinMua.GetCategoryString(ID);
            HiddenCategory.Value = ID.ToString();
        }
        
        public int CategoryID
        {
            get
            {
                return int.Parse(HiddenCategory.Value);
            }
        }

        public string Title
        {
            get
            {
                return txtTitle.Text;
            }
        }

        public string Keyword
        {
            get
            {
                return txtKeyword.Text;
            }
        }

        public string Description
        {
            get
            {
                return txtDescription.Text;
            }
        }

        public int ExpiryTime
        {
            get
            {
                return int.Parse(cbExpiryTime.SelectedValue.ToString());
            }
        }
       
        public int BuyingFrequency
        {
            get
            {
                return int.Parse(cbBuyingFrequency.SelectedValue.ToString());
            }
        }

        public string BuyingFrequencyString
        {
            get
            {
                return cbBuyingFrequency.SelectedItem.Text;
            }
        }

        public int QuantityRequired
        {
            get
            {
                int result;
                if (int.TryParse(txtQuantityRequired.Text, out result))
                {
                    return result;
                }
                else
                {
                    return 0;
                }
            }
        }

        public int? UnitQuantityRequired
        {
            get
            {
                int result;
                if (int.TryParse(cbQuantityRequired.SelectedValue.ToString(), out result))
                {
                    return result;
                }
                else
                {
                    return null;
                }
            }
        }

        public int AnnualPurchaseVolume
        {
            get
            {
                int result;
                if (int.TryParse(txtAnnualPurchaseVolume.Text, out result))
                {
                    return result;
                }
                else
                {
                    return 0;
                }
            }
        }

        public int? UnitAnnualPurchaseVolume
        {
            get
            {
                int result;
                if (int.TryParse(cbAnnualPurchaseVolume.SelectedValue.ToString(), out result))
                {
                    return result;
                }
                else
                {
                    return null;
                }
            }
        }

        public string ShippingTerms
        {
            get
            {
                return txtShippingTerms.Text;
            }
        }

        public string DestinationPort
        {
            get
            {
                return txtDestinationPort.Text;
            }
        }

        public int[] Payment
        {
            get
            {
                int[] arrPayment = new int[7];
                for (int i = 0; i < PaymentCheckBoxList.Items.Count; i++)
                {
                    if (PaymentCheckBoxList.Items[i].Selected)
                    {
                        arrPayment[i] = int.Parse(PaymentCheckBoxList.Items[i].Value);
                    }
                }
                return arrPayment;
            }
        }
    }
}